package com.wgh.product.controller;

import com.wgh.common.vo.Result;
import com.wgh.product.dto.ProductAttributeDTO;
import com.wgh.product.entity.shop.ProductAttribute;
import com.wgh.product.service.ProductAttributeService;
import com.wgh.product.converter.ProductAttributeConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品属性控制器
 * 提供商品属性相关的RESTful API
 */
@RestController
@RequestMapping("/api/product-attribute")
public class ProductAttributeController {

    @Autowired
    private ProductAttributeService productAttributeService;
    
    @Autowired
    private ProductAttributeConverter productAttributeConverter;
    
    /**
     * 根据分类ID查询属性列表
     */
    @GetMapping("/category/{categoryId}")
    public Result<List<ProductAttributeDTO>> getAttributesByCategoryId(@PathVariable Long categoryId) {
        try {
            List<ProductAttribute> attributes = productAttributeService.getAttributesByCategoryId(categoryId);
            List<ProductAttributeDTO> attributeDTOs = productAttributeConverter.toDTOList(attributes);
            return Result.success(attributeDTOs);
        } catch (Exception e) {
            return Result.error("获取分类属性列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据分类ID列表查询属性列表
     */
    @GetMapping("/categories")
    public Result<List<ProductAttributeDTO>> getAttributesByCategoryIds(@RequestParam List<Long> categoryIds) {
        try {
            List<ProductAttribute> attributes = productAttributeService.getAttributesByCategoryIds(categoryIds);
            List<ProductAttributeDTO> attributeDTOs = productAttributeConverter.toDTOList(attributes);
            return Result.success(attributeDTOs);
        } catch (Exception e) {
            return Result.error("获取多分类属性列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 查询属性名称是否存在
     */
    @GetMapping("/check")
    public Result<Boolean> checkAttributeExists(@RequestParam String name, @RequestParam Long categoryId) {
        try {
            boolean exists = productAttributeService.checkAttributeExists(name, categoryId);
            return Result.success(exists);
        } catch (Exception e) {
            return Result.error("检查属性名称失败：" + e.getMessage());
        }
    }
    
    /**
     * 批量删除属性
     */
    @DeleteMapping("/batch")
    public Result<Boolean> deleteBatch(@RequestParam List<Long> ids) {
        try {
            boolean result = productAttributeService.deleteBatch(ids);
            return result ? Result.success(true) : Result.error("批量删除属性失败");
        } catch (Exception e) {
            return Result.error("批量删除属性失败：" + e.getMessage());
        }
    }
    
    /**
     * 批量更新属性状态
     */
    @PutMapping("/status/batch")
    public Result<Boolean> updateStatusBatch(@RequestParam List<Long> ids, @RequestParam Integer status) {
        try {
            boolean result = productAttributeService.updateStatusBatch(ids, status);
            return result ? Result.success(true) : Result.error("批量更新属性状态失败");
        } catch (Exception e) {
            return Result.error("批量更新属性状态失败：" + e.getMessage());
        }
    }
    
    /**
     * 分页查询属性
     */
    @GetMapping("/page")
    public Result<List<ProductAttributeDTO>> getAttributePage(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Long categoryId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            List<ProductAttribute> attributes = productAttributeService.getAttributePage(keyword, categoryId, pageNum, pageSize);
            List<ProductAttributeDTO> attributeDTOs = productAttributeConverter.toDTOList(attributes);
            return Result.success(attributeDTOs);
        } catch (Exception e) {
            return Result.error("分页查询属性失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据ID查询属性详情
     */
    @GetMapping("/{id}")
    public Result<ProductAttributeDTO> getAttributeById(@PathVariable Long id) {
        try {
            ProductAttribute attribute = productAttributeService.getById(id);
            if (attribute == null) {
                return Result.success(null);
            }
            ProductAttributeDTO attributeDTO = productAttributeConverter.toDTO(attribute);
            return Result.success(attributeDTO);
        } catch (Exception e) {
            return Result.error("获取属性详情失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据ID列表批量查询属性
     */
    @GetMapping("/batch")
    public Result<List<ProductAttributeDTO>> getAttributesByIds(@RequestParam List<Long> ids) {
        try {
            List<ProductAttribute> attributes = productAttributeService.listByIds(ids);
            List<ProductAttributeDTO> attributeDTOs = productAttributeConverter.toDTOList(attributes);
            return Result.success(attributeDTOs);
        } catch (Exception e) {
            return Result.error("批量查询属性失败：" + e.getMessage());
        }
    }
    
    /**
     * 新增属性
     */
    @PostMapping
    public Result<Boolean> addAttribute(@RequestBody ProductAttributeDTO productAttributeDTO) {
        try {
            ProductAttribute productAttribute = productAttributeConverter.toEntity(productAttributeDTO);
            boolean result = productAttributeService.save(productAttribute);
            return result ? Result.success(true) : Result.error("新增属性失败");
        } catch (Exception e) {
            return Result.error("新增属性失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新属性
     */
    @PutMapping
    public Result<Boolean> updateAttribute(@RequestBody ProductAttributeDTO productAttributeDTO) {
        try {
            ProductAttribute productAttribute = productAttributeConverter.toEntity(productAttributeDTO);
            boolean result = productAttributeService.updateById(productAttribute);
            return result ? Result.success(true) : Result.error("更新属性失败");
        } catch (Exception e) {
            return Result.error("更新属性失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除属性
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteAttribute(@PathVariable Long id) {
        try {
            boolean result = productAttributeService.removeById(id);
            return result ? Result.success(true) : Result.error("删除属性失败");
        } catch (Exception e) {
            return Result.error("删除属性失败：" + e.getMessage());
        }
    }
}